<template>
    <div>
        <el-row>
            <el-col :span="8" push="8">
                <el-divider content-position="left">library</el-divider>
                <el-form ref="form" :model="form" label-width="80px">
                    <el-form-item label="用户名">
                        <el-input v-model="form.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="form.password" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="验证码">
                        <el-col :span="17">
                            <el-input v-model="form.code" @keyup.enter.native="toLogin"></el-input>
                        </el-col>
                        <el-col :span="7">
                            <img :src="codeUrl" alt="" @click="getAgain">
                        </el-col>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="toLogin">登录</el-button>
                        <el-button type="primary" @click="toRegister">注册</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import {login,createCodeImage} from "@/api";

    export default {
        name: "Login",
        data(){
            return {
                form:{
                    username: '',
                    password: '',
                    code: ''
                },
                codeUrl: createCodeImage + '?' + new Date().getTime()
            }
        },
        methods:{
            toRegister(){
                this.$router.push('/register')
            },
            async toLogin(){
                let result = await login({username:this.form.username,password:this.form.password,code:this.form.code})
                console.log(result)
                this.$router.push('/books')
            },
            getAgain(){
                this.codeUrl = createCodeImage + '?' + new Date().getTime()
            }
        }
    }
</script>

<style scoped>

</style>
